<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
    <script type="text/javascript" src="bootstrap/js/jquery.js"></script>
    <script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
    <form>
        <input type="hidden" id="id">
        <div class="form-group">
            <label for="exampleInputEmail1">用户名</label>
            <input type="text" class="form-control" id="exampleInputEmail1" placeholder="Email">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword1">新密码</label>
            <input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
        </div>
        <div class="form-group">
            <label for="exampleInputPassword2">重复密码</label>
            <input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password">
        </div>
        <input id="sub" class="btn btn-success form-control" type="button" value="修改">

    </form>
</div>
</body>
</html>
<script type="text/javascript">
    $(function () {
        var id = GetQueryString("id")
        $.ajax({
            url:'user/getUserById?id='+id,
            success:function (data) {
                $("#id").val(data.id);
                 $("#exampleInputEmail1").val(data.username);
                 $("#exampleInputPassword1").val(data.password);
                 $("#exampleInputPassword2").val(data.password);
            }

        })
    })

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) {
            return unescape(r[2]);
        }
        return null;
    }


    $("#sub").click(function () {
        var id = $("#id").val();
        var userName = $("#exampleInputEmail1").val();
        var passWord = $("#exampleInputPassword1").val();
        var repassWord = $("#exampleInputPassword2").val();
        if (userName.length == 0){
            alert("用户名不可为空");
            return;//目的是让程序不再往下执行
        }
        if (passWord.length == 0){
            alert("密码不可为空");
            return;
        }
        if (passWord != repassWord){
            alert("两次输入的密码必须相同");
            return;
        }
        //封装数据并提供给后端
        var user = {
            "id":id,
            "username":userName,
            "password":passWord
        }
        //将实体类转换成json
        var userJson = JSON.stringify(user)

        //提交给后端接口
        $.post({
            url:'user/updateUserById',
            data:userJson,
            contentType:'application/json',
            success:function (data) {
                alert(data)
                console.log(data)
            }
        });
    })
</script>